package api

import (
	"github.com/gin-gonic/gin"
	"syscall"
	"web-collect-server/entity/common/response"
)

type DiskStatus struct {
	All  float32 `json:"all"`
	Used float32 `json:"used"`
	Free float32 `json:"free"`
}

// DiskUsage disk usage of path/disk
func DiskUsage(path string) (disk DiskStatus) {
	fs := syscall.Statfs_t{}
	err := syscall.Statfs(path, &fs)
	if err != nil {
		return
	}
	disk.All = float32(fs.Blocks*uint64(fs.Bsize)) / 1024 / 1024 / 1024
	disk.Free = float32(fs.Bfree*uint64(fs.Bsize)) / 1024 / 1024 / 1024
	disk.Used = disk.All - disk.Free
	return
}

func InitApi(r *gin.Engine) {
	// 给 一个 api 的路由组
	api := r.Group("/api")
	InitFolderRouter(api)
	InitDeviceRouter(api)
	InitLoginRouter(api)
	api.GET("test", test)
	api.GET("capacity", getCapacity)
}

func getCapacity(c *gin.Context) {
	var params TreeParams
	err := c.ShouldBindQuery(&params)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	usage := DiskUsage(params.Path)
	response.OkWithData(usage, c)
}

func test(c *gin.Context) {
	response.OkWithMessage("dudu", c)
}
